//
//  YKSearchDataByGoodsViewController.m
//  YunKang
//
//  Created by leoking870 on 2017/11/2.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKSearchDataByGoodsViewController.h"
#import "YKDataHeaderView.h"
#import "YKMultiSelectDropdownView.h"
#import "YKSaleModel.h"
#import "YKClientListHeaderView.h"
#import "YKSelectDropdownView.h"
#import "YKSelectView.h"
#import "YKSaleItem.h"
#import "YKSearchViewController.h"
#import "YKPopupView.h"
#import "YKSearchOnlineViewController.h"
@interface YKSearchDataByGoodsViewController ()
@property (nonatomic, strong) YKSelectView *goodsSelectView;
@property (nonatomic, strong) YKDataHeaderView *headerView;
@property (nonatomic, strong) NSArray *goodsSummaryDetail;
@property(nonatomic, strong) YKClientListHeaderView *listHeaderView;
@property (nonatomic, strong) UILabel *leftLabel;
@property (nonatomic, strong) UILabel *rightLabel;
@property(nonatomic, strong) UILabel *rowLabel;
@property(nonatomic, strong) UIView *bottomView;
@end

@implementation YKSearchDataByGoodsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"按品种查询";
    self.view.backgroundColor = [UIColor whiteColor];
    @weakify(self)
    YKDataHeaderView *headerView = [[YKDataHeaderView alloc]initWithType:YKDataHeaderViewTypeYearMonthDay];
    headerView.dateChangeBlock = ^(NSString *start, NSString *end) {
        
        [self_weak_ loadData];
    };
    self.headerView = headerView;
    [self.view addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(self.view.mas_top);
    }];
    
    YKSaleModel *allModels = [[YKSaleModel alloc]init];
    allModels.GOODS_ID = @"";
    allModels.GOODS_NAME = @"全部";
    NSArray *goods = [@[allModels] arrayByAddingObjectsFromArray:[CommonDataManager sharedInstance].goods];
    
    self.goodsSelectView = [[YKSelectView alloc]initWithTitle:@"选择品种" row:goods initialText:goods.firstObject];
    self.goodsSelectView.enabled = NO;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onSelectTapped:)];
    self.goodsSelectView.userInteractionEnabled = YES;
    [self.goodsSelectView addGestureRecognizer:tap];
    self.goodsSelectView.selectHandler = ^(id<YKDropdown> value) {
        [self loadData];
    };
    [self.view addSubview:self.goodsSelectView];
    [self.goodsSelectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(headerView.mas_bottom);
    }];

    self. listHeaderView = [[YKClientListHeaderView alloc] initWithTitles:@[@"区域",@"销量",@"金额"] weights:@[@1,@1,@1]];
    self.listHeaderView.textFont = [UIFont systemFontOfSize:16];
    [self.view addSubview:self.listHeaderView];
    [self.listHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(self.view.mas_top);
    }];
    self.listHeaderView.backgroundColor = [UIColor tf_colorWithHexString:@"#1EADFC"];

    self.listHeaderView.textFont = [UIFont systemFontOfSize:16];
    
    
    self.bottomView = [[UIView alloc]init];
    [self.view addSubview:self.bottomView];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.bottom.trailing.equalTo(self.view);
        make.height.mas_equalTo(40);
    }];
    
    self.rowLabel = [[UILabel alloc]init];
    self.rowLabel.text = @"共0行";
    [self.bottomView addSubview:self.rowLabel];
    [self.rowLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.bottomView.mas_centerY);
    }];
    self.rowLabel.tf_weight = 1;
    
    UILabel *leftLabel = [[UILabel alloc]init];
    leftLabel.text = @"销量：0";
    [self.bottomView addSubview:leftLabel];
    [leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.bottomView.mas_centerY);
    }];
    leftLabel.tf_weight = 2;
    UILabel *rightLabel = [[UILabel alloc]init];
    rightLabel.text = @"金额：0";
    [self.bottomView addSubview:rightLabel];
    [rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.bottomView.mas_centerY);
    }];
    rightLabel.tf_weight = 2;
    [@[self.rowLabel, leftLabel, rightLabel] mas_alignWeightlyAlongAxis:MASAxisTypeHorizontal fixedSpace:@0 leadingTo:self.view.mas_leading leadSpace:8 trailingTo:self.view.mas_trailing tailSpace:8];
    
    
    self.leftLabel = leftLabel;
    self.rightLabel = rightLabel;
    self.rowLabel.font = [UIFont boldSystemFontOfSize:16];
    leftLabel.font = [UIFont boldSystemFontOfSize:16];
    rightLabel.font = [UIFont boldSystemFontOfSize:16];

    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(self.listHeaderView.mas_bottom);
        make.bottom.equalTo(self.bottomView.mas_top);
    }];
    
    
    
    
    [self loadData];
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self.view bringSubviewToFront:self.tableView];

    UIBarButtonItem *filterItem = [[UIBarButtonItem alloc] initWithTitle:@"筛选" style:UIBarButtonItemStylePlain target:self action:@selector(onFilterItemTapped:)];
    self.navigationItem.rightBarButtonItem = filterItem;
}

- (void)onFilterItemTapped:(UIBarButtonItem *)item {
    if (item.tag == 0) {
        [UIView animateWithDuration:.3f animations:^{
            [self.listHeaderView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.leading.trailing.equalTo(self.view);
                make.top.equalTo(self.goodsSelectView.mas_bottom);
            }];
            [self.view layoutIfNeeded];
        }];

        item.tag = 1;
    } else{
        item.tag = 0;
        [UIView animateWithDuration:.3f animations:^{
            [self.listHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.leading.trailing.equalTo(self.view);
                make.top.equalTo(self.view.mas_top);
            }];
            [self.view layoutIfNeeded];
        }];
    }

}

- (void)onSelectTapped:(id)sender {
    @weakify(self)
    YKSearchOnlineViewController *vc = [YKSearchOnlineViewController new];
    __weak YKSearchOnlineViewController *wvc = vc;
    vc.selectDrugComplete = ^(id<YKDropdown> model) {
       self_weak_.goodsSelectView.selectedRow = model;
        [YKPopupView dismissViewController:wvc];
    };
    vc.dataSource = self.goodsSelectView.data;
    [YKPopupView showPopupViewWithViewController:vc height:self.view.tf_height - 150];
}
- (void)loadData {
    [self tf_showStateView:kTFViewStateLoading inView:self.tableView];
    [TFHTTPMANAER POST:@"manage/goodsSummaryDetail.htm" parameters:@{@"startDate":self.headerView.start, @"endDate": self.headerView.end, @"goodsId":_goodsSelectView.selectedRow.code?:@""} success:^(id responseObject) {
        
        [self tf_showStateView:kTFViewStateNone inView:self.tableView];
        [self.tableViewManager removeAllSections];
        self.goodsSummaryDetail = [NSArray yy_modelArrayWithClass:[YKSaleModel class] json:responseObject[@"goodsSummaryDetail"]];
        
        TFTableViewSection * section = [[TFTableViewSection alloc]init];
        section.headerHeight = .1f;
        section.footerHeight = .1f;
        [section addItemsFromArray:[self.goodsSummaryDetail tf_mapUsingBlock:^id _Nullable(id  _Nonnull obj, NSInteger idx) {
            return [YKSaleItem itemWithModel:obj];
        }]];
        
        CGFloat sale = 0, gold = 0;
        for (YKSaleModel *model in self.goodsSummaryDetail) {
            gold += model.SALES;
            sale += model.SALES_NUM.floatValue;
        }
        self.leftLabel.text = [NSString stringWithFormat:@"销量:%.0f",sale];
        self.rightLabel.text = [NSString stringWithFormat:@"金额:%.2f", gold];
        self.rowLabel.text = [NSString stringWithFormat:@"共%d行", self.goodsSummaryDetail.count];
        if (section.items.count == 0) {
            [self tf_showStateView:kTFViewStateNoData inView:self.tableView];
        }
        [self.tableViewManager addSection:section];
        
        [self.tableView reloadData];
    } failure:^(id responseObject, NSError *error) {
        [self tf_showStateView:kTFViewStateNetError inView:self.tableView];
    }];
}

@end
